package com.mt.api.modules.service.impl;


import com.google.common.collect.Lists;
import com.mt.api.constants.enums.MenuTypeEnum;
import com.mt.api.modules.domain.Menu;
import com.mt.api.modules.domain.RoleMenu;
import com.mt.api.modules.domain.vo.TreeSelectDataVo;
import com.mt.api.modules.manager.IMenuManager;
import com.mt.api.modules.manager.IRoleMenuManager;
import com.mt.api.modules.service.IMenuService;
import com.mt.common.response.WebResponse;
import com.mt.common.response.WebResponseUtils;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author: plover
 * @Date: 2022-04-30
 * @Description:
 */
@Service
@RequiredArgsConstructor
public class MenuServiceImpl implements IMenuService {

    private final IMenuManager menuManager;
    private final IRoleMenuManager roleMenuManager;


    /**
     * [{
     * "text": "销售",
     * "badge": null,
     * "children": [{
     * "text": "销售单",
     * "id": 1
     * },
     * {
     * "text": "销售记录",
     * "id": 2
     * }
     * ]
     * }]
     *
     * @return
     */
    @Override
    public WebResponse getTreeSelectData(Integer roleId) {
        List<Menu> menuList = menuManager.getList();
        List<Menu> categoryMenuList = menuList.stream().filter(menu -> Objects.equals(menu.getType(), MenuTypeEnum.CATEGORY.getCode())).collect(Collectors.toList());
        List<Menu> functionMenuList = menuList.stream().filter(menu -> Objects.equals(menu.getType(), MenuTypeEnum.FUNCTION.getCode())).collect(Collectors.toList());

        // 如果传入了角色ID，则需要设置badge，如没传入，则badge默认为null
        List<Integer> roleMenuIdList = Lists.newArrayList();
        if (Objects.nonNull(roleId)) {
            if (Objects.equals(1, roleId)) {
                List<Menu> menus = menuManager.getList();
                roleMenuIdList = menus.stream().map(Menu::getId).collect(Collectors.toList());
            } else {
                List<RoleMenu> roleMenuList = roleMenuManager.getByRoleId(roleId);
                roleMenuIdList = roleMenuList.stream().map(RoleMenu::getMenuId).collect(Collectors.toList());
            }
        }

        List<TreeSelectDataVo> voList = Lists.newArrayList();
        for (Menu menu : categoryMenuList) {
            TreeSelectDataVo vo = new TreeSelectDataVo();
            vo.setText(menu.getTitle());
            List<Menu> childrenMenuList = functionMenuList.stream().filter(menu1 -> Objects.equals(menu1.getPid(), menu.getId())).
                    sorted(Comparator.comparing(Menu::getMenuSort)).collect(Collectors.toList());
            List<Integer> menuIdList = childrenMenuList.stream().map(Menu::getId).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(roleMenuIdList)) {
                Collection<Integer> intersection = CollectionUtils.intersection(menuIdList, roleMenuIdList);
                vo.setBadge(intersection.size() == 0 ? null : intersection.size());
            }
            List<TreeSelectDataVo.Children> childrenList = childrenMenuList.stream().map(menu1 -> {
                TreeSelectDataVo.Children children = new TreeSelectDataVo.Children();
                children.setId(menu1.getId());
                children.setText(menu1.getTitle());
                return children;
            }).collect(Collectors.toList());
            vo.setChildren(childrenList);
            voList.add(vo);
        }
        return WebResponseUtils.successList(voList);
    }
}
